package com.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Paths;
import java.util.Collections;
import java.util.Scanner;

public class mybatisPlusGenerator {
    // 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new RuntimeException("请输入正确的" + tip + "！");
    }

    private static final String outputDir = "D:\\IdeaProjects\\oneiot-component\\src\\main\\java";

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://10.12.60.42:30735/capabilityintegration_dev?useUnicode=true&characterEncoding=UTF8&useSSL=false", "root", "ZA!2wxCD")
                .globalConfig(builder -> builder
                        .author("huyang")
                        .outputDir(outputDir)
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent("com.cmiot.open.capabilityintegration")
                        .entity("entity")
                        .mapper("dao")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper")
                )
                .strategyConfig(builder -> {
                    builder.addInclude(scanner("表名"))
                            .addTablePrefix("tb_open_");

                    builder.entityBuilder()
                            .enableLombok()
                            .addTableFills(
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.UPDATE)
                            );
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}
